package cn.qihua.dao.impl;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import cn.qihua.dao.ISysRoleDAO;
import cn.qihua.model.SysRole;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysRole entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.qihua.model.SysRole
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class SysRoleDAO extends BaseHibernateDAO implements ISysRoleDAO {
	private static final Logger log = LoggerFactory.getLogger(SysRoleDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#save(cn.qihua.model.SysRole)
	 */
	@Override
	public void save(SysRole transientInstance) {
		log.debug("saving SysRole instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#delete(cn.qihua.model.SysRole)
	 */
	@Override
	public void delete(SysRole persistentInstance) {
		log.debug("deleting SysRole instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findById(java.lang.Long)
	 */
	@Override
	public SysRole findById(java.lang.Integer id) {
		log.debug("getting SysRole instance with id: " + id);
		try {
			SysRole instance = (SysRole) getCurrentSession().get(
					"cn.qihua.model.SysRole", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findByExample(cn.qihua.model.SysRole)
	 */
	@Override
	public List findByExample(SysRole instance) {
		log.debug("finding SysRole instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("cn.qihua.model.SysRole")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysRole instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SysRole as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findByRole(java.lang.Object)
	 */
	@Override
	public List findByText(Object text) {
		return findByProperty(TEXT, text);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findByDescription(java.lang.Object)
	 */
	@Override
	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findByResourceIds(java.lang.Object)
	 */
	@Override
	public List findByResourceIds(Object resourceIds) {
		return findByProperty(RESOURCE_IDS, resourceIds);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findByAvailable(java.lang.Object)
	 */
	@Override
	public List findByAvailable(Object available) {
		return findByProperty(AVAILABLE, available);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all SysRole instances");
		try {
			String queryString = "from SysRole";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#merge(cn.qihua.model.SysRole)
	 */
	@Override
	public SysRole merge(SysRole detachedInstance) {
		log.debug("merging SysRole instance");
		try {
			SysRole result = (SysRole) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#attachDirty(cn.qihua.model.SysRole)
	 */
	@Override
	public void attachDirty(SysRole instance) {
		log.debug("attaching dirty SysRole instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.ISysRoleDAO#attachClean(cn.qihua.model.SysRole)
	 */
	@Override
	public void attachClean(SysRole instance) {
		log.debug("attaching clean SysRole instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ISysRoleDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ISysRoleDAO) ctx.getBean("SysRoleDAO");
	}
}